/**
 * 
 */
package com.ao.andou.db;

import com.ao.andou.AppConstant;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

/**
 * Sqlite helper for Andou
 * Create database for only once, and init some tables
 * @author pentiumao
 * 
 */
/*package*/ class AndouSqliteHelper extends SQLiteOpenHelper {

    /* for log */
    private static final String TAG = AndouSqliteHelper.class.getSimpleName();

    /*
     * Create a helper object to create, open, and/or manage a database. The
     * database is not actually created or opened until one of
     * getWritableDatabase() or getReadableDatabase() is called.
     */
    public AndouSqliteHelper(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
     * .SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        // create account table
        final String createAccountTable = 
                "CREATE TABLE " + AppConstant.T_ACCOUNT 
                + "("
                // auto increment primary key(row id)
                + "_ID INTEGER PRIMARY KEY ASC AUTOINCREMENT, "
                // local user name
                + AppConstant.USER_NAME + " TEXT PRIMARY KEY, "
                // user password
                + AppConstant.USER_PASSWORD + " TEXT, "
                // user id(use for douban api)
                + AppConstant.USER_ID + " TEXT, "
                // request token
                + AppConstant.REQUEST_TOKEN + " TEXT, "
                // access token
                + AppConstant.ACCESS_TOKEN + " TEXT, "
                // token secret
                + AppConstant.TOKEN_SECRET + " TEXT, "
                // valid flag(true or false)
                + AppConstant.VALID + " TEXT" + ")";
        try {
            db.execSQL(createAccountTable);
        } catch (SQLException e) {
            // TODO: handle exception
            Log.e(TAG, e.getMessage());
        }
        Log.d(TAG, "table account created");
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
     * .SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // FIXME: implement this next version
    }

}
